/*
 * write_cg.c
 *
 *  Created on: 2012/09/04
 *      Author: shenming
 */

#include <stdio.h>
#include "sqlite3.h"
#include "db_def.h"
#include "core.h"
#include "core_internal.h"
#include "Id.h"
#include "../../CCore/table/table.h"
#include "../../CCore/parser/cc/cc.h"
#include "../../CCore/table/core_table.h"
#include "../../CCore/parser/coredef.h"

/*TBL_MEMBER*/
static INT insertMember(sqlite3 *db) {
	INT rc = 0;
	sqlite3_stmt * stmt;
	const char *zTail;
	PSZ sql_insert = NULL;
	BOOL bIsErr = FALSE;

	AST max = 0;
	AST i = 0;

	rc = sqlite3_exec(db, "begin;", 0, 0, 0);
	if (rc != SQLITE_OK) {
		return rc;
	}

	max = stMemberSymbolAccess.getMax();

	sql_insert = sqlite3_mprintf("INSERT INTO %q VALUES(?,?,?,?,?,?,?,?);",
			TBL_MEMBER);
	rc = sqlite3_prepare(db, sql_insert, -1, &stmt, &zTail);
	if (rc != SQLITE_OK || stmt == NULL ) {
		if (sql_insert != NULL ) {
			sqlite3_free(sql_insert);
		}
		if (stmt != NULL ) {
			sqlite3_finalize(stmt);
		}
		return COEXIT_ERROR;
	}

	for (i = 1; i <= max; i++) {
		sqlite3_bind_int(stmt, P_TABLE_INDEX, i);

		sqlite3_bind_int(stmt, MEMBER_CLASS,
				stMemberSymbolAccess.symClass.get(i));

		sqlite3_bind_int(stmt, MEMBER_TYPE, stMemberSymbolAccess.type.get(i));

		sqlite3_bind_text(stmt, MEMBER_NAME, stMemberSymbolAccess.name.get(i),
				-1, SQLITE_STATIC );

		sqlite3_bind_int(stmt, MEMBER_VALUE, stMemberSymbolAccess.value.get(i));

		sqlite3_bind_int(stmt, MEMBER_CODESCOPE,
				stMemberSymbolAccess.codeScp.get(i));

		sqlite3_bind_int(stmt, MEMBER_NEXT, stMemberSymbolAccess.next.get(i));

		sqlite3_bind_int(stmt, MEMBER_MEMOFS,
				stMemberSymbolAccess.memOffset.get(i));

		rc = sqlite3_step(stmt);
		if (rc != SQLITE_DONE) {
			bIsErr = TRUE;
			printf("%s", sqlite3_errmsg(db));
			break;
		}
		sqlite3_reset(stmt);
	}

	if (bIsErr) {
		rc = sqlite3_exec(db, "rollback;", 0, 0, 0);
	} else {
		rc = sqlite3_exec(db, "commit;", 0, 0, 0);
	}

	if (sql_insert != NULL ) {
		sqlite3_free(sql_insert);
	}
	if (stmt != NULL ) {
		rc = sqlite3_finalize(stmt);
	}
	return rc;
}

/*TBL_CONSTANT*/
static INT insertConstant(sqlite3 *db) {
	INT rc = 0;
	sqlite3_stmt * stmt;
	const char *zTail;
	PSZ sql_insert = NULL;
	BOOL bIsErr = FALSE;

	AST max = 0;
	AST i = 0;

	rc = sqlite3_exec(db, "begin;", 0, 0, 0);
	if (rc != SQLITE_OK) {
		return rc;
	}

	max = stConstantAccess.getMax();

	sql_insert = sqlite3_mprintf("INSERT INTO %q VALUES(?,?,?,?,?,?,?);",
			TBL_CONSTANT);
	rc = sqlite3_prepare(db, sql_insert, -1, &stmt, &zTail);
	if (rc != SQLITE_OK || stmt == NULL ) {
		if (sql_insert != NULL ) {
			sqlite3_free(sql_insert);
		}
		if (stmt != NULL ) {
			sqlite3_finalize(stmt);
		}
		return COEXIT_ERROR;
	}

	for (i = 1; i <= max; i++) {
		sqlite3_bind_int(stmt, P_TABLE_INDEX, i);

		sqlite3_bind_int(stmt, CONSTANT_B_FLOATING,
				stConstantAccess.isFloating(i));

		sqlite3_bind_int(stmt, CONSTANT_SIZE, stConstantAccess.getSize(i));

		sqlite3_bind_double(stmt, CONSTANT_DOUBLEVALUE, 0);
		sqlite3_bind_int(stmt, CONSTANT_B_SIGNED, 0);
		sqlite3_bind_int(stmt, CONSTANT_B_NEG, 0);
		sqlite3_bind_int64(stmt, CONSTANT_INTVALUE, 0);
		if (stConstantAccess.isFloating(i)) {
			sqlite3_bind_double(stmt, CONSTANT_DOUBLEVALUE,
					stConstantAccess.getFloating(i));
		} else {
			sqlite3_bind_int(stmt, CONSTANT_B_SIGNED,
					stConstantAccess.isSigned(i));

			sqlite3_bind_int(stmt, CONSTANT_B_NEG,
					stConstantAccess.isNegative(i));

			sqlite3_bind_int64(stmt, CONSTANT_INTVALUE,
					stConstantAccess.getInteger(i).u8Remainder);
		}

		rc = sqlite3_step(stmt);
		if (rc != SQLITE_DONE) {
			bIsErr = TRUE;
			printf("%s", sqlite3_errmsg(db));
			break;
		}
		sqlite3_reset(stmt);
	}

	if (bIsErr) {
		rc = sqlite3_exec(db, "rollback;", 0, 0, 0);
	} else {
		rc = sqlite3_exec(db, "commit;", 0, 0, 0);
	}

	if (sql_insert != NULL ) {
		sqlite3_free(sql_insert);
	}
	if (stmt != NULL ) {
		rc = sqlite3_finalize(stmt);
	}
	return rc;
}

/*TBL_GBLLINKAGE*/
static INT insertGblLinkage(sqlite3 *db) {
	INT rc = 0;
	sqlite3_stmt * stmt;
	const char *zTail;
	PSZ sql_insert = NULL;
	BOOL bIsErr = FALSE;

	AST max = 0;
	AST i = 0;

	rc = sqlite3_exec(db, "begin;", 0, 0, 0);
	if (rc != SQLITE_OK) {
		return rc;
	}

	max = stGblLinkageAccess.getMax();

	sql_insert = sqlite3_mprintf("INSERT INTO %q VALUES(?,?,?,?,?);",
			TBL_GBLLINKAGE);
	rc = sqlite3_prepare(db, sql_insert, -1, &stmt, &zTail);
	if (rc != SQLITE_OK || stmt == NULL ) {
		if (sql_insert != NULL ) {
			sqlite3_free(sql_insert);
		}
		if (stmt != NULL ) {
			sqlite3_finalize(stmt);
		}
		return COEXIT_ERROR;
	}

	for (i = 1; i <= max; i++) {
		sqlite3_bind_int(stmt, P_TABLE_INDEX, i);

		sqlite3_bind_int(stmt, GBLLINK_SYMDEFINFO,
				stGblLinkageAccess.vari.get(i));

		sqlite3_bind_int(stmt, GBLLINK_B_REF, stGblLinkageAccess.refer.get(i));

		sqlite3_bind_int(stmt, GBLLINK_B_EXTERNAL,
				stGblLinkageAccess.bExternal.get(i));

		sqlite3_bind_int(stmt, GBLLINK_B_FUNCTION,
				stGblLinkageAccess.bFunction.get(i));

		rc = sqlite3_step(stmt);
		if (rc != SQLITE_DONE) {
			bIsErr = TRUE;
			printf("%s", sqlite3_errmsg(db));
			break;
		}
		sqlite3_reset(stmt);
	}

	if (bIsErr) {
		rc = sqlite3_exec(db, "rollback;", 0, 0, 0);
	} else {
		rc = sqlite3_exec(db, "commit;", 0, 0, 0);
	}

	if (sql_insert != NULL ) {
		sqlite3_free(sql_insert);
	}
	if (stmt != NULL ) {
		rc = sqlite3_finalize(stmt);
	}
	return rc;
}

/*TBL_FUNCTIONIDINFO*/
static INT insertFunctionIdInfo(sqlite3 *db) {
	INT rc = 0;
	sqlite3_stmt * stmt;
	const char *zTail;
	PSZ sql_insert = NULL;
	BOOL bIsErr = FALSE;

	AST max = 0;
	AST i = 0;

	rc = sqlite3_exec(db, "begin;", 0, 0, 0);
	if (rc != SQLITE_OK) {
		return rc;
	}

	max = stFunctionInfoAccess.getMax();

	sql_insert = sqlite3_mprintf("INSERT INTO %q VALUES(?,?,?,?,?,?,?,?);",
			TBL_FUNCTIONIDINFO);
	rc = sqlite3_prepare(db, sql_insert, -1, &stmt, &zTail);
	if (rc != SQLITE_OK || stmt == NULL ) {
		if (sql_insert != NULL ) {
			sqlite3_free(sql_insert);
		}
		if (stmt != NULL ) {
			sqlite3_finalize(stmt);
		}
		return COEXIT_ERROR;
	}

	for (i = 1; i <= max; i++) {
		sqlite3_bind_int(stmt, P_TABLE_INDEX, i);

		sqlite3_bind_int(stmt, FUNC_SYM_CLASS,
				stFunctionInfoAccess.symClass.get(i));

		sqlite3_bind_int(stmt, FUNC_LINKAGE_INFO,
				stFunctionInfoAccess.linkage.get(i));

		sqlite3_bind_int(stmt, FUNC_TYPE_INFO,
				stFunctionInfoAccess.type.get(i));

		sqlite3_bind_text(stmt, FUNC_SYMBOL_NAME,
				stFunctionInfoAccess.name.get(i), -1, SQLITE_STATIC );

		sqlite3_bind_int(stmt, FUNC_ENTRY,
				stFunctionInfoAccess.functionEntry.get(i));

		sqlite3_bind_int(stmt, FUNC_EXIT,
				stFunctionInfoAccess.functionExit.get(i));

		sqlite3_bind_int(stmt, FUNC_CODE_SCOPE_INFO,
				stFunctionInfoAccess.codeScp.get(i));

		rc = sqlite3_step(stmt);
		if (rc != SQLITE_DONE) {
			bIsErr = TRUE;
			printf("%s", sqlite3_errmsg(db));
			break;
		}
		sqlite3_reset(stmt);
	}

	if (bIsErr) {
		rc = sqlite3_exec(db, "rollback;", 0, 0, 0);
	} else {
		rc = sqlite3_exec(db, "commit;", 0, 0, 0);
	}

	if (sql_insert != NULL ) {
		sqlite3_free(sql_insert);
	}
	if (stmt != NULL ) {
		rc = sqlite3_finalize(stmt);
	}
	return rc;
}

/*TBL_VARIABLEINFO*/
static INT insertVariableInfo(sqlite3 *db) {
	INT rc = 0;
	sqlite3_stmt * stmt;
	const char *zTail;
	PSZ sql_insert = NULL;
	BOOL bIsErr = FALSE;

	AST max = 0;
	AST i = 0;

	rc = sqlite3_exec(db, "begin;", 0, 0, 0);
	if (rc != SQLITE_OK) {
		return rc;
	}

	max = stVariableInfoAccess.getMax();

	sql_insert = sqlite3_mprintf(
			"INSERT INTO %q VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);",
			TBL_VARIABLEINFO);
	rc = sqlite3_prepare(db, sql_insert, -1, &stmt, &zTail);
	if (rc != SQLITE_OK || stmt == NULL ) {
		if (sql_insert != NULL ) {
			sqlite3_free(sql_insert);
		}
		if (stmt != NULL ) {
			sqlite3_finalize(stmt);
		}
		return COEXIT_ERROR;
	}

	for (i = 1; i <= max; i++) {
		sqlite3_bind_int(stmt, P_TABLE_INDEX, i);

		sqlite3_bind_int(stmt, VAR_SYM_CLASS,
				stVariableInfoAccess.symClass.get(i));

		sqlite3_bind_int(stmt, VAR_LINKAGE_INFO,
				stVariableInfoAccess.linkage.get(i));

		sqlite3_bind_int(stmt, VAR_TYPE_INFO, stVariableInfoAccess.type.get(i));

		sqlite3_bind_text(stmt, VAR_SYMBOL_NAME,
				stVariableInfoAccess.name.get(i), -1, SQLITE_STATIC );

		sqlite3_bind_int(stmt, VAR_CODE_SCOPE_INFO,
				stVariableInfoAccess.codeScp.get(i));

		sqlite3_bind_int(stmt, VAR_DATA_SECTION_INFO,
				stVariableInfoAccess.dataSection.get(i));

		sqlite3_bind_int(stmt, VAR_INITIAL_CMDBEGIN,
				stVariableInfoAccess.initialCmdBegin.get(i));

		sqlite3_bind_int(stmt, VAR_INITIAL_CMDEND,
				stVariableInfoAccess.initialCmdEnd.get(i));

		sqlite3_bind_int(stmt, VAR_VALUE_INFO,
				stVariableInfoAccess.value.get(i));

		sqlite3_bind_int64(stmt, VAR_OFFSET,
				stVariableInfoAccess.offset.get(i));

		sqlite3_bind_int64(stmt, VAR_LENGTH,
				stVariableInfoAccess.length.get(i));

		sqlite3_bind_int(stmt, VAR_B_SIZEVARI,
				stVariableInfoAccess.bSizeVariable.get(i));

		sqlite3_bind_int(stmt, VAR_CHILD, stVariableInfoAccess.child.get(i));

		sqlite3_bind_int(stmt, VAR_PARENT, stVariableInfoAccess.parent.get(i));

		sqlite3_bind_int(stmt, VAR_NEXT, stVariableInfoAccess.next.get(i));

		sqlite3_bind_int(stmt, VAR_PREV, stVariableInfoAccess.prev.get(i));

		rc = sqlite3_step(stmt);
		if (rc != SQLITE_DONE) {
			bIsErr = TRUE;
			printf("%s", sqlite3_errmsg(db));
			break;
		}
		sqlite3_reset(stmt);
	}

	if (bIsErr) {
		rc = sqlite3_exec(db, "rollback;", 0, 0, 0);
	} else {
		rc = sqlite3_exec(db, "commit;", 0, 0, 0);
	}

	if (sql_insert != NULL ) {
		sqlite3_free(sql_insert);
	}
	if (stmt != NULL ) {
		rc = sqlite3_finalize(stmt);
	}
	return rc;
}

/*TBL_COMMANDLIST*/
static INT insertCmdList(sqlite3 *db) {
	INT rc = 0;
	sqlite3_stmt * stmt;
	const char *zTail;
	PSZ sql_insert = NULL;
	BOOL bIsErr = FALSE;

	AST max = 0;
	AST i = 0;

	rc = sqlite3_exec(db, "begin;", 0, 0, 0);
	if (rc != SQLITE_OK) {
		return rc;
	}

	max = stCmdListAccess.getMax();

	sql_insert = sqlite3_mprintf(
			"INSERT INTO %q VALUES(?,?,?,?,?,?,?,?,?,?,?);", TBL_COMMANDLIST);
	rc = sqlite3_prepare(db, sql_insert, -1, &stmt, &zTail);
	if (rc != SQLITE_OK || stmt == NULL ) {
		if (sql_insert != NULL ) {
			sqlite3_free(sql_insert);
		}
		if (stmt != NULL ) {
			sqlite3_finalize(stmt);
		}
		return COEXIT_ERROR;
	}

	for (i = 1; i <= max; i++) {
		sqlite3_bind_int(stmt, P_TABLE_INDEX, i);

		sqlite3_bind_int(stmt, CMDLIST_KIND, stCmdListAccess.opKind.get(i));

		//TODO
		sqlite3_bind_int(stmt, CMDLIST_B_INLOOP, 1);

		sqlite3_bind_int(stmt, CMDLIST_CODESCOPE,
				stCmdListAccess.codeScp.get(i));

		sqlite3_bind_int(stmt, CMDLIST_TYPE, stCmdListAccess.type.get(i));

		sqlite3_bind_int(stmt, CMDLIST_FUNC, stCmdListAccess.function.get(i));

		sqlite3_bind_int(stmt, CMDLIST_LEFT,
				stCmdListAccess.leftOperand.get(i));

		sqlite3_bind_int(stmt, CMDLIST_RIGHT,
				stCmdListAccess.rightOperand.get(i));

		sqlite3_bind_int(stmt, CMDLIST_PREV, stCmdListAccess.before.get(i));

		sqlite3_bind_int(stmt, CMDLIST_NEXT, stCmdListAccess.next.get(i));

		sqlite3_bind_text(stmt, CMDLIST_NAME, stCmdListAccess.label.get(i), -1,
				SQLITE_STATIC );

		rc = sqlite3_step(stmt);
		if (rc != SQLITE_DONE) {
			bIsErr = TRUE;
			printf("%s", sqlite3_errmsg(db));
			break;
		}
		sqlite3_reset(stmt);
	}

	if (bIsErr) {
		rc = sqlite3_exec(db, "rollback;", 0, 0, 0);
	} else {
		rc = sqlite3_exec(db, "commit;", 0, 0, 0);
	}

	if (sql_insert != NULL ) {
		sqlite3_free(sql_insert);
	}
	if (stmt != NULL ) {
		rc = sqlite3_finalize(stmt);
	}
	return rc;
}

/*TBL_OPERAND*/
static INT insertOperand(sqlite3 *db) {
	INT rc = 0;
	sqlite3_stmt * stmt;
	const char *zTail;
	PSZ sql_insert = NULL;
	BOOL bIsErr = FALSE;

	AST max = 0;
	AST i = 0;

	rc = sqlite3_exec(db, "begin;", 0, 0, 0);
	if (rc != SQLITE_OK) {
		return rc;
	}

	max = stOperandAccess.getMax();

	sql_insert = sqlite3_mprintf("INSERT INTO %q VALUES(?,?,?);", TBL_OPERAND);
	rc = sqlite3_prepare(db, sql_insert, -1, &stmt, &zTail);
	if (rc != SQLITE_OK || stmt == NULL ) {
		if (sql_insert != NULL ) {
			sqlite3_free(sql_insert);
		}
		if (stmt != NULL ) {
			sqlite3_finalize(stmt);
		}
		return COEXIT_ERROR;
	}

	for (i = 1; i <= max; i++) {
		sqlite3_bind_int(stmt, P_TABLE_INDEX, i);

		sqlite3_bind_int(stmt, OPERAND_KIND, stOperandAccess.kind.get(i));

		sqlite3_bind_int(stmt, OPERAND_PARAM, stOperandAccess.parameter.get(i));

		rc = sqlite3_step(stmt);
		if (rc != SQLITE_DONE) {
			bIsErr = TRUE;
			printf("%s", sqlite3_errmsg(db));
			break;
		}
		sqlite3_reset(stmt);
	}

	if (bIsErr) {
		rc = sqlite3_exec(db, "rollback;", 0, 0, 0);
	} else {
		rc = sqlite3_exec(db, "commit;", 0, 0, 0);
	}

	if (sql_insert != NULL ) {
		sqlite3_free(sql_insert);
	}
	if (stmt != NULL ) {
		rc = sqlite3_finalize(stmt);
	}
	return rc;
}

/*TBL_LINKTYPEINFO*/
static INT insertLinkType(sqlite3 *db) {
	INT rc = 0;
	sqlite3_stmt * stmt;
	const char *zTail;
	PSZ sql_insert = NULL;
	BOOL bIsErr = FALSE;

	AST max = 0;
	AST i = 0;

	rc = sqlite3_exec(db, "begin;", 0, 0, 0);
	if (rc != SQLITE_OK) {
		return rc;
	}

	max = stLinkTypeInfoAccess.getMax();

	sql_insert = sqlite3_mprintf(
			"INSERT INTO %q VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);",
			TBL_LINKTYPEINFO);
	rc = sqlite3_prepare(db, sql_insert, -1, &stmt, &zTail);
	if (rc != SQLITE_OK || stmt == NULL ) {
		if (sql_insert != NULL ) {
			sqlite3_free(sql_insert);
		}
		if (stmt != NULL ) {
			sqlite3_finalize(stmt);
		}
		return COEXIT_ERROR;
	}

	for (i = 1; i <= max; i++) {
		sqlite3_bind_int(stmt, P_TABLE_INDEX, i);

		sqlite3_bind_int(stmt, LINKTYPE_KIND,
				stLinkTypeInfoAccess.typeKind.get(i));

		sqlite3_bind_int(stmt, LINKTYPE_SIGNED,
				stLinkTypeInfoAccess.signFlag.get(i));

		sqlite3_bind_int(stmt, LINKTYPE_LONGSHORT,
				stLinkTypeInfoAccess.longShortFlg.get(i));

		sqlite3_bind_int(stmt, LINKTYPE_INTFLOAT,
				stLinkTypeInfoAccess.intFloat.get(i));

		sqlite3_bind_int(stmt, LINKTYPE_TYPEQUAL,
				stLinkTypeInfoAccess.typeQual.get(i));

		sqlite3_bind_int(stmt, LINKTYPE_B_UNDEFINED,
				stLinkTypeInfoAccess.bSizeUnDefined.get(i));

		sqlite3_bind_int(stmt, LINKTYPE_B_VARSIZE,
				stLinkTypeInfoAccess.bVariableSize.get(i));

		sqlite3_bind_int64(stmt, LINKTYPE_SIZE,
				stLinkTypeInfoAccess.size.get(i));

		sqlite3_bind_int(stmt, LINKTYPE_ORIGIN,
				stLinkTypeInfoAccess.elementType.get(i));

		sqlite3_bind_int(stmt, LINKTYPE_NOQUAL,
				stLinkTypeInfoAccess.noQualType.get(i));

		sqlite3_bind_int(stmt, LINKTYPE_MEMCHAIN,
				stLinkTypeInfoAccess.memChain.get(i));

		sqlite3_bind_text(stmt, LINKTYPE_NAME,
				stLinkTypeInfoAccess.tagName.get(i), -1, SQLITE_STATIC );

		sqlite3_bind_int(stmt, LINKTYPE_NEXT, stLinkTypeInfoAccess.next.get(i));

		sqlite3_bind_int(stmt, LINKTYPE_CNT, stLinkTypeInfoAccess.cnter.get(i));

		rc = sqlite3_step(stmt);
		if (rc != SQLITE_DONE) {
			bIsErr = TRUE;
			printf("%s", sqlite3_errmsg(db));
			break;
		}
		sqlite3_reset(stmt);
	}

	if (bIsErr) {
		rc = sqlite3_exec(db, "rollback;", 0, 0, 0);
	} else {
		rc = sqlite3_exec(db, "commit;", 0, 0, 0);
	}

	if (sql_insert != NULL ) {
		sqlite3_free(sql_insert);
	}
	if (stmt != NULL ) {
		rc = sqlite3_finalize(stmt);
	}
	return rc;
}

/*TBL_TRANSUNIT*/
static INT insertTransUnit(sqlite3 *db) {
	INT rc = 0;
	sqlite3_stmt * stmt;
	const char *zTail;
	PSZ sql_insert = NULL;
	BOOL bIsErr = FALSE;

	AST max = 0;
	AST i = 0;

	rc = sqlite3_exec(db, "begin;", 0, 0, 0);
	if (rc != SQLITE_OK) {
		return rc;
	}

	max = stCoreTranslationUnitAccess.getMax();

	sql_insert = sqlite3_mprintf("INSERT INTO %q VALUES(?,?,?,?,?,?);",
			TBL_TRANSUNIT);
	rc = sqlite3_prepare(db, sql_insert, -1, &stmt, &zTail);
	if (rc != SQLITE_OK || stmt == NULL ) {
		if (sql_insert != NULL ) {
			sqlite3_free(sql_insert);
		}
		if (stmt != NULL ) {
			sqlite3_finalize(stmt);
		}
		return COEXIT_ERROR;
	}

	for (i = 1; i <= max; i++) {
		sqlite3_bind_int(stmt, P_TABLE_INDEX, i);

		sqlite3_bind_int(stmt, TU_FILEINDEX,
				stCoreTranslationUnitAccess.fileindex.get(i));

		sqlite3_bind_int(stmt, TU_DEC_CHAIN,
				stCoreTranslationUnitAccess.declarationchain.get(i));

		sqlite3_bind_int(stmt, TU_VARI_MAX,
				stCoreTranslationUnitAccess.preVariMax.get(i));

		sqlite3_bind_int(stmt, TU_FUNC_MAX,
				stCoreTranslationUnitAccess.preFuncMax.get(i));

		sqlite3_bind_int(stmt, TU_LINK_MAX,
				stCoreTranslationUnitAccess.preLinkageMax.get(i));

		rc = sqlite3_step(stmt);
		if (rc != SQLITE_DONE) {
			bIsErr = TRUE;
			printf("%s", sqlite3_errmsg(db));
			break;
		}
		sqlite3_reset(stmt);
	}

	if (bIsErr) {
		rc = sqlite3_exec(db, "rollback;", 0, 0, 0);
	} else {
		rc = sqlite3_exec(db, "commit;", 0, 0, 0);
	}

	if (sql_insert != NULL ) {
		sqlite3_free(sql_insert);
	}
	if (stmt != NULL ) {
		rc = sqlite3_finalize(stmt);
	}
	return rc;
}

/*insert cg tables*/
INT DLL_EXPORT insertCgTables(sqlite3 *db) {
	INT rc = 0;

	rc = insertTransUnit(db);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = insertMember(db);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = insertConstant(db);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = insertGblLinkage(db);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = insertFunctionIdInfo(db);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = insertVariableInfo(db);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = insertCmdList(db);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = insertOperand(db);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = insertLinkType(db);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}
	return COEXIT_SUCCESS;
}

